package com.zhuoya.healthcard.util;

import android.util.*;
import android.content.*;
import android.media.*;

public final class SoundPlayer {
    private static final boolean DEBUG = false;
    private static final long LOAD_SOUND_MILLIS = 100L;
    public static final int MAX_STREAMS = 1;
    private static final String TAG;
    public static long playTime;
    public static float rate;
    private static SoundPlayer sSoundPlayer;
    private SoundPool mSoundPool;
    private SparseIntArray mSoundPoolCache;

    static {
        TAG = SoundPlayer.class.getSimpleName();
        SoundPlayer.playTime = 0L;
        SoundPlayer.rate = 1.0f;
    }

    private SoundPlayer() {
        this.mSoundPool = new SoundPool(1, 3, 0);
        this.mSoundPoolCache = new SparseIntArray();
        SoundPlayer.playTime = 0L;
    }

    public static void play(final Context context, final int n) {
        if (SoundPlayer.sSoundPlayer == null) {
            SoundPlayer.sSoundPlayer = new SoundPlayer();
        }
        final int value = SoundPlayer.sSoundPlayer.mSoundPoolCache.get(n);
        if (value == 0) {
            final int load = SoundPlayer.sSoundPlayer.mSoundPool.load(context, n, 1);
            SoundPlayer.sSoundPlayer.mSoundPoolCache.put(n, load);
            if (APIUtils.hasFroyo()) {
                SoundPlayer.sSoundPlayer.mSoundPool.setOnLoadCompleteListener((SoundPool.OnLoadCompleteListener) new SoundPool.OnLoadCompleteListener() {
                    public void onLoadComplete(final SoundPool soundPool, final int n, final int n2) {
                        if (n2 == 0 && load == n) {
                            try {
                                SoundPlayer.playTime = System.currentTimeMillis();
                                SoundPlayer.sSoundPlayer.mSoundPool.play(load, 1.0f, 1.0f, 1, 0, SoundPlayer.rate);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                });
            } else {
                try {
                    Thread.currentThread().join(100L);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                SoundPlayer.playTime = System.currentTimeMillis();
                SoundPlayer.sSoundPlayer.mSoundPool.play(load, 1.0f, 1.0f, 1, 0, SoundPlayer.rate);
            }
            return;
        }
        try {
            SoundPlayer.sSoundPlayer.mSoundPool.play(value, 1.0f, 1.0f, 1, 0, SoundPlayer.rate);
        } catch (Exception ex2) {
            ex2.printStackTrace();
        }
    }

    public static void release() {
        final SoundPlayer sSoundPlayer = SoundPlayer.sSoundPlayer;
        if (sSoundPlayer != null) {
            for (int i = 0; i < sSoundPlayer.mSoundPoolCache.size(); ++i) {
                final SoundPlayer sSoundPlayer2 = SoundPlayer.sSoundPlayer;
                sSoundPlayer2.mSoundPool.unload(sSoundPlayer2.mSoundPoolCache.valueAt(i));
            }
            SoundPlayer.sSoundPlayer.mSoundPool.release();
            final SoundPlayer sSoundPlayer3 = SoundPlayer.sSoundPlayer;
            sSoundPlayer3.mSoundPool = null;
            sSoundPlayer3.mSoundPoolCache.clear();
            SoundPlayer.sSoundPlayer.mSoundPoolCache = null;
            SoundPlayer.sSoundPlayer = null;
        }
    }
}
